package com.pig4cloud.pigx.contract.executer.order.matcher.impl;

import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.executer.order.matcher.OrderMatcherManager;
import com.pig4cloud.pigx.contract.mapper.ContractOrderMapper;
import com.pig4cloud.pigx.contract.mongo.entity.ContractOrderMongo;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 默认订单撮合处理器
 *
 * @since 2022/8/17 21:26
 */
@Component
public class LimitOrderMatcher extends AbstractOrderMatcher
		implements InitializingBean {

	protected final OrderMatcherManager orderMatcherManager;

	/**
	 * 有参构造函数
	 * <p>同时给父级对象赋值
	 *
	 * @since 2022/8/17 20:51
	 */
	public LimitOrderMatcher(ContractOrderMapper contractOrderMapper,
							 MongoTemplate mongo,
							 AmqpTemplate rabbit,
							 CommonUtil commonUtil,
							 ContractUtils contractUtils,
							 OrderMatcherManager orderMatcherManager) {
		super(contractOrderMapper, mongo, rabbit, commonUtil, contractUtils);
		this.orderMatcherManager = orderMatcherManager;
	}

	/**
	 * 当前对象创建后
	 *
	 * @since 2022/8/17 14:36
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		// 注册到manager
		orderMatcherManager.getMatcherList().add(this);
	}

	/**
	 * 判断是否支持
	 *
	 * @param taker 订单
	 * @return {@link boolean}
	 * @since 2022/8/17 11:20
	 */
	@Override
	public boolean support(ContractOrderMongo taker) {
		return Objects.equals(CommonDictEnum.YesOrNo.NO.getValue(), taker.getIsRobot())
				&& (Objects.equals(ContractOrderEnum.Type.LIMIT.getValue(), taker.getType())
				|| Objects.equals(ContractOrderEnum.Type.LIQUIDATE.getValue(), taker.getType()));
	}

	/**
	 * 查询订单薄时，设置额外筛选条件
	 *
	 * @param criteria 查询条件封装
	 * @param taker    订单
	 * @return {@link Criteria}
	 * @since 2022/8/18 15:41
	 */
	@Override
	protected Criteria setExtraCriteria(Criteria criteria, ContractOrderMongo taker) {
		// 不能是本人的订单
		criteria.and("userId").ne(taker.getUserId())
				// 不能是机器人的订单
				.and("isRobot").is(CommonDictEnum.YesOrNo.NO.getValue());

		// 如果是买单，寻找价格低的订单薄
		if (Objects.equals(CommonDictEnum.YesOrNo.YES.getValue(), taker.getIsBuy())) {
			criteria.and("price").lte(taker.getPrice());
		} else {
			// 如果是卖单，寻找价格高的订单薄
			criteria.and("price").gte(taker.getPrice());
		}

		return criteria;
	}

	/**
	 * 判断两个订单能否撮合
	 *
	 * @param taker 新订单
	 * @param maker 对手盘订单
	 * @return {@link boolean}
	 * @since 2022/8/18 16:08
	 */
	@Override
	public boolean matchable(ContractOrderMongo taker, ContractOrderMongo maker) {
		if (!super.matchable(taker, maker)) {
			return false;
		}

		// taker是否是买单
		if (Objects.equals(CommonDictEnum.YesOrNo.YES.getValue(), taker.getIsBuy())) {
			// 判断买价 >= 卖价
			return taker.getPrice().compareTo(maker.getPrice()) >= 0;
		} else {
			// 判断卖价 <= 买价
			return taker.getPrice().compareTo(maker.getPrice()) <= 0;
		}
	}

	/**
	 * 撮合成功
	 *
	 * @param taker 订单
	 * @since 2022/8/17 11:31
	 */
	@Override
	public void success(ContractOrderMongo taker) {
		// 修改isNew字段为false
		contractUtils.changeIsNew(taker);
	}
}
